﻿ 
 IF EXISTS(SELECT NAME FROM SYSOBJECTS WHERE NAME='Proc_GetMaterial' AND TYPE='P')
BEGIN
  DROP PROCEDURE Proc_GetMaterial
END
GO
 
 
 
 set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




/* MBB 2010-5-20
库存物资分页数据
*/
CREATE Procedure Proc_GetMaterial --1,1,'',''
 	@PageIndex int,
	@PageSize int,
	@MaterialsName varchar(50),
	@Region varchar(20)
 AS
DECLARE @SQL nvarchar(1000)
DECLARE @RecordCount int,@P1 INT
DECLARE @Where VARCHAR(200)
IF @PageIndex >1 SET @PageIndex = (@PageIndex-1)*@PageSize +1
--IF @PageIndex = 1  SET @PageIndex =1

SET @Where = ''
IF ((@MaterialsName IS NOT NULL) AND (RTRIM(LTRIM(@MaterialsName))<>''))
BEGIN
  SET @Where = @Where +  ' AND MaterialsName LIKE ''%'+ @MaterialsName + '%'''
END
IF ((LEN(@Where)>0) AND (LEFT(@Where,5)=' AND')) 
BEGIN
	SET @Where = @Where +  ' AND Location = '''+ @Region + ''''
END
IF ((LEN(@Where)>0) AND (LEFT(@Where,5)=' AND')) 
BEGIN
	SET @Where = Right(@Where,LEN(@Where)-5)
END
SET @SQL = N'SELECT A.*,B.Name CategoryName FROM  Materials A LEFT JOIN CommonCode B ON A.Category = B.Code ORDER BY MaterialsID DESC'
IF (LEN(@Where)>0)
BEGIN
  SET @SQL = 'SELECT A.*,B.Name CategoryName FROM  Materials A LEFT JOIN CommonCode B ON A.Category = B.Code WHERE ' + @Where + ' ORDER BY MaterialsID DESC'
END
PRINT @SQL
EXEC sp_cursoropen @P1 OUTPUT, @SQL, @scrollopt = 1, @ccopt = 335873, @rowcount = @RecordCount OUTPUT 
IF (@P1 != 0)
BEGIN
  EXEC sp_cursorfetch @P1, 32,@PageIndex , @PageSize 
  EXEC sp_cursorclose @P1
END
select @RecordCount
return @@ERROR





